package cn.yuanqiao.archive.shuzihua.service.impl;

import cn.yuanqiao.archive.appManager.domain.*;
import cn.yuanqiao.archive.appManager.mapper.*;
import cn.yuanqiao.archive.shuzihua.service.IDangAnZhengJiService;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DangAnZhengJiServiceImpl implements IDangAnZhengJiService {
    @Autowired
    private TenantCategoryMapper categoryMapper;
    @Autowired
    private TenantYqItemMapper itemMapper;
    @Autowired
    private TenantYqFolderMapper folderMapper;
    @Autowired
    private TenantMdataListMapper mdataListMapper;
    @Autowired
    private TenantYqdaMapper yqdaMapper;

    @Override
    @Transactional
    public void jieshou(Long categoryId, Long[] ids) {
        TenantCategory category = new TenantCategory();
        category = categoryMapper.selectTenantCategoryByID(categoryId,category.getTableName());
        Map<String,String> column = new HashMap<>();
        column.put("STATE","12");
        //处理项目层
        if(category.getType() == 3){
            TenantYqItem item = new TenantYqItem();
            item.setColNames(column);
            itemMapper.updateItemByIds(item,ids); //主表
            item.setTableName(category.getItemTableName());
            itemMapper.updateItemDisperseByIds(item,ids);  //分表
            ids = folderMapper.getFolderIdsByItemIds(ids,category.getFolderTableName(),new TenantItemFolder().getTableName());//查询项目下的卷
            if(ids== null || ids.length==0){
                throw new ServiceException("项目下没有卷信息，接收失败！");
            }
        }

        //处理卷层
        if(category.getType() == 3 || category.getType() == 1){
            TenantYqFolder folder = new TenantYqFolder();
            folder.setColNames(column);
            folderMapper.updateYqFolderByIds(folder,ids); //主表
            folder.setTableName(category.getFolderTableName());
            folderMapper.updateYqFolderDisperseByIds(folder,ids);
            ids = yqdaMapper.getDaIdsByFolderIds(ids,category.getFileTableName(),new TenantYqdaFolder().getTableName());//查询卷下的件
        }
        //处理件层
        List<List<Long>> lists = ListUtils.subList(Arrays.asList(ids), 5000);
        for(List<Long> idds:lists){
            TenantYqda yqda = new TenantYqda();
            yqda.setColNames(column);
            yqdaMapper.updateYqdaByIds(yqda,idds);
            yqda.setTableName(category.getFileTableName());
            yqdaMapper.updateYqdaDisperseByIds(yqda,idds);
        }
    }
}
